/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.edit;

import com.daikit.daikit4gxt.client.ui.edit.BeforeOpenEvent.BeforeOpenHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.data.shared.ListStore;


/**
 * Event before Open
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <M>
 *           the type of objects within the store associated to the opened component
 */
@SuppressWarnings("rawtypes")
public class BeforeOpenEvent<M> extends GwtEvent<BeforeOpenHandler>
{
	/**
	 * Handler type.
	 */
	private static Type<BeforeOpenHandler> TYPE;

	private final ListStore<M> store;

	/**
	 * @return the store
	 */
	public ListStore<M> getStore()
	{
		return store;
	}

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	public static Type<BeforeOpenHandler> getType()
	{
		if (TYPE == null)
		{
			TYPE = new Type<BeforeOpenHandler>();
		}
		return TYPE;
	}

	/**
	 * Constructor
	 * 
	 * @param store
	 *           the store
	 */
	public BeforeOpenEvent(final ListStore<M> store)
	{
		this.store = store;
	}


	@Override
	public Type<BeforeOpenHandler> getAssociatedType()
	{
		return TYPE;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void dispatch(final BeforeOpenHandler handler)
	{
		handler.onBeforeOpen(this);
	}

	/**
	 * Handler for {@link BeforeOpenEvent} events.
	 * 
	 * @param <M>
	 *           the type of objects within the store associated to the opened component
	 */
	public interface BeforeOpenHandler<M> extends EventHandler
	{
		/**
		 * Called after a component is open.
		 * 
		 * @param event
		 *           the {@link BeforeOpenEvent} that was fired
		 */
		void onBeforeOpen(BeforeOpenEvent<M> event);
	}

	/**
	 * A widget that implements this interface is a public source of {@link BeforeOpenEvent} events.
	 * 
	 * @param <M>
	 *           the type of objects within the store associated to the opened component
	 */
	public interface HasBeforeOpenHandlers<M>
	{

		/**
		 * Adds a {@link BeforeOpenHandler} handler for {@link BeforeOpenEvent} events.
		 * 
		 * @param handler
		 *           the handler
		 * @return the registration for the event
		 */
		HandlerRegistration addBeforeOpenHandler(BeforeOpenHandler<M> handler);
	}
}
